#include<cstring>
#include <iostream>
#include <vector>  
#include <algorithm>  
using namespace std;
string str="";
vector<string> result;  
class Observer
{
    // 用虚函数实现
    public: 
        virtual void Update(int) = 0;
        virtual void sort_result() = 0;
        virtual void input() = 0;
        virtual void output() = 0;
        virtual vector<string> get_allstr() = 0;
        virtual void union_result(vector<string>& ) = 0;
};
 
class TureObserver1:public Observer
{
    public: 
        // string str="";
        // vector<string> result;
        void Update(int m)
        {
            m_state = m;
            std::cout<<"TureObserver1  Update  m = "<< m_state <<std::endl;
            cout<<"Please writting the string:"<<endl;
            input();
            vector<string> tmp = get_allstr();
            union_result(tmp);
            sort_result();
            output();
        }
        void union_result(vector<string>& r){
	        result.insert(result.end(),r.begin(),r.end());
        }
        void sort_result(){
            sort(result.begin(),result.end(),[](string a,string b){
                return a[0]<b[0];//快排+匿名函数，类似于小顶堆的实现
            });
        }
        void output(){
            cout<<"***********************************************************************"<<endl;
            cout<<"This time you get the flower result"<<endl;
            for(int i = 0;i<result.size();i++){
                cout<<result[i]<<endl;
            }
            cout<<"#######################################################################"<<endl<<endl<<endl;
        }
        void input(){
            string s;
            cout<<"please input the origin string:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
            getline(cin,s);//获取一行有空格的数据
            str += s;//进行字符串合并
        }
        vector<string> get_allstr(){
            vector<string> ans;
            vector<string> vec;
            string s="";
            for(int i = 0;i<str.size();i++){
                if(str[i]==' '&&s.size()) vec.push_back(s),s.clear();
                else if(str[i]!=' ') s += str[i];
            }
            if(s.size()!=0) vec.push_back(s);
            int n = vec.size();
            for(int i = 0;i<n;i++){//可以进行n次移位,前i个字进行移位
                string tmp="";
                int j = i+1;
                while(j<n){
                    tmp += vec[j++]+" ";
                }
                j = 0;
                while(j<=i){
                    tmp += vec[j++];
                    if(j!=i+1) tmp+=" ";
                }
                ans.push_back(tmp);
            }
            str = "";//将字符串置为空，共享空间的做法。
            return ans;
        }
    private:
        int m_state;
};
 
class TureObserver2:public Observer
{
    public: 
        void Update(int m)
        {
            m_state = m;
            std::cout<<"TureObserver2  Update  m = "<< m_state <<std::endl;
            cout<<"Please writting the string:"<<endl;
            input();
            vector<string> tmp = get_allstr();
            union_result(tmp);
            sort_result();
            output();
        }
        void union_result(vector<string>& r){
	        result.insert(result.end(),r.begin(),r.end());
        }
        void sort_result(){
            sort(result.begin(),result.end(),[](string a,string b){
                return a[0]<b[0];//快排+匿名函数，类似于小顶堆的实现
            });
        }
        void output(){
            cout<<"***********************************************************************"<<endl;
            cout<<"This time you get the flower result"<<endl;
            for(int i = 0;i<result.size();i++){
                cout<<result[i]<<endl;
            }
            cout<<"#######################################################################"<<endl<<endl<<endl;
        }
        void input(){
            string s;
            cout<<"please input the origin string:>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
            getline(cin,s);//获取一行有空格的数据
            str += s;//进行字符串合并
        }
        vector<string> get_allstr(){
            vector<string> ans;
            vector<string> vec;
            string s="";
            for(int i = 0;i<str.size();i++){
                if(str[i]==' '&&s.size()) vec.push_back(s),s.clear();
                else if(str[i]!=' ') s += str[i];
            }
            if(s.size()!=0) vec.push_back(s);
            int n = vec.size();
            for(int i = 0;i<n;i++){//可以进行n次移位,前i个字进行移位
                string tmp="";
                int j = i+1;
                while(j<n){
                    tmp += vec[j++]+" ";
                }
                j = 0;
                while(j<=i){
                    tmp += vec[j++];
                    if(j!=i+1) tmp+=" ";
                }
                ans.push_back(tmp);
            }
            str = "";//将字符串置为空，共享空间的做法。
            return ans;
        }
    private:
        int m_state;
};
 
class Subject
{
    public:
        Subject()
        {
    
        }
        virtual ~Subject()
        {
    
        }
        virtual void Register(Observer *pObserver)
        {
            ObserverList.push_back(pObserver);
        }
        virtual void UnRegister(Observer *pObserver)
        {
            std::vector < Observer *>::iterator iObserver = std::find(ObserverList.begin(), ObserverList.end(), pObserver); 
    
            if(iObserver != ObserverList.end())
            {
                ObserverList.erase(iObserver);
                std::cout<<" unregister success !"<< std::endl;
            }
            else
            {
                std::cout<<"  not find in list"<< std::endl;
            }
        }
        virtual void Notify(int m)//接收到事件后的处理
        {
            std::vector < Observer *>::iterator iObserver = ObserverList.begin();  
            while(iObserver != ObserverList.end())  
            {     
                (*iObserver)->Update(m);
                iObserver++;  
            }  
        }
    private:
        std::vector < Observer *> ObserverList;
};
 
class TrueSubject:public Subject
{
 
};
 
int main()
{
    // 用来实现事件的传递
	TrueSubject subject;
	TureObserver2  Observer2;
	TureObserver1  Observer1;
 
	subject.Register(&Observer2);
	subject.Register(&Observer1);
 
	subject.Notify(5);
 
	subject.UnRegister(&Observer2);
	subject.UnRegister(&Observer1);
}
